<template>
	<view class="wx-login">
		<view v-if="is_register == 1" class="xw-login-form">
			<form @submit="sublogin">
				<view class="xw-login-form-item">
					<view class="xw-login-form-label">手机号</view>
					<input class="xw-login-form-input" maxlength="11" placeholder="请填写手机号" type="number" name="phone" v-model="phone" />
					<view class="login-form-icon login-form-seepass" @click="phone=''" v-if="phone.length>0">
						<uni-icons type="clear" size="26" color="#B9CCE0"></uni-icons>
					</view>
				</view>
				<view class="xw-login-form-item">
					<view class="xw-login-form-label">验证码</view>
					<input class="xw-login-form-input" placeholder="请填写验证码" v-model="code" name="code" type="number" />
					<view class="wx-btn wx-btn-info" @click="getMsgCode()" v-if="!loading">获取验证码</view>
					<view class="wx-btn wx-btn-info" v-else>{{time}}秒后重试</view>
				</view>
				<view class="xw-login-form-item">
					<view class="xw-login-form-label">密码</view>
					<input class="xw-login-form-input" placeholder="请输入密码" name="password" :password="showPassword"
						type="text" value="" />
					<view class="login-form-icon login-form-seepass" @click="changePassword">
						<image src="../../static/img/l03.png" mode="aspectFill" v-if="showPassword"></image>
						<image src="../../static/img/l04.png" mode="aspectFill" v-else></image>
					</view>
				</view>
				<view class="xw-login-form-item">
					<view class="xw-login-form-label">昵称</view>
					<input class="xw-login-form-input" placeholder="请输入你的昵称" type="text" name="nickName" value="" maxlength="30" />
				</view>
				<button class="wx-btn wx-btn-info xw-login-form-btn" form-type="submit">注册</button>
				<button class="wx-btn wx-btn-info xw-login-form-btn_h" @click="goDown()">下载</button>
				<view class="login-agree">
					<view class="login-agree-checkd" @click="agree = !agree">
						<label for="agree">
							<checkbox id="agree" style="transform:scale(0.7)" :checked="agree" />
							<text class="login-agree-btn">已阅读并同意</text>
						</label>
					</view>
					<view class="login-agree-text" @click="goagreement()">《隐私及服务协议》</view>
				</view>
			</form>
		</view>
		<slideCode v-if="slideCode[0].show" :session_id="session_id[0]" send_type="register" @close="slideCode[0].show = false" @success="slideCode_success(0, $event)"></slideCode>
	</view>
</template>
<script>
var app = getApp();
	import slideCode from '@/components/slideCode/slideCode.vue'
	export default {
		data() {
			return {
				is_register: app.globalData.appconfig ? app.globalData.appconfig.member_allow_register : 0,
				code:'',
				loading: false,
				timer: null,
				time: 60,
				logintype: 0,
				phone: '',
				cid: '',
				showPassword: true,
				agree: false,
                inviteCode:'',//邀请码
				session_id: [],
				slideCode: [
					{show: false}
				],
			}
		},
		onLoad: function(option) {
			var self = this;
			var ua = window.navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) == 'micromessenger'){
				self.inviteCode = option.invite;
				if(this.$fc.is_empty(app.globalData.appconfig)){
					self.reloadconfig();
				}else{
					if(self.is_register == 0){
						self.goDown();
					}
				}
			}else{
			    self.is_register = 0;
				uni.showToast({
					title: '请在微信内打开',
					icon: 'none'
				})
			}
		},
		methods: {
		    reloadconfig(){
				var self = this;
				if(this.$fc.is_empty(app.globalData.appconfig)){
					setTimeout(()=>{
						self.reloadconfig();
					},500);
				}else{
					self.is_register = app.globalData.appconfig.member_allow_register;
					if(self.is_register == 0){
						self.goDown();
					}
				}
		    },
			changePassword() {
				var self = this;
				self.showPassword = !self.showPassword;
			},
			goDown(){
				uni.navigateTo({
					url: './download'
				})
			},
			//在线协议
			goagreement() {
				var self = this;
				self.$http.request({
					url: '/common/getAgreement',
					success: (res) => {
						if (res.data.code == 200) {
							window.open(res.data.data);
						}
					}
				});
			},
			getMsgCode() {
				var self = this;
			    if (self.phone == "") {
                    uni.showToast({
						title:'请填写手机号',
						icon:'none'
					})
					return false;
				}
				var reg = /^1[0-9]{10,10}$/;
				if(!self.phone||!reg.test(self.phone)){
					uni.showToast({
						title:'请输入正确的手机号',
						icon:'none'
					})
					return
				}
				self.session_id[0] = self.phone * 1;
				self.slideCode[0].show = true;
			},
			//发送验证码回调
			slideCode_success: function(id,ret){
				var self = this;
				if (ret.code == 200) {
					self.loading = true
    				self.timer = setInterval(() => {
    					self.time--
    					if (self.time <= 0) {
    						clearInterval(self.timer)
    						self.loading = false
    						self.time = 60
    					}
    				}, 1000);
				}
			},
			rMathfloor(min, max) { //返回包括最大/小值
				return Math.floor(Math.random() * (max - min + 1)) + min
			},
			sublogin(e) {
				var self = this;
				var rules = {
					phone: {
						rules: [{
							checkType: "required",
							errorMsg: "请填写手机号码"
						}, {
							checkType: "phone",
							errorMsg: "请填写正确的手机号码"
						}]
					},
					password: {
						rules: [{
							checkType: "required",
							errorMsg: "请输入密码"
						}, {
							checkType: "string",
							checkRule: "6,20",
							errorMsg: "密码至少输入6-20位"
						}]
					},
					nickName: {
						rules: [{
							checkType: "required",
							errorMsg: "请输入昵称"
						}, {
							checkType: "string",
							checkRule: "1,20",
							errorMsg: "昵称至少输入1-20位"
						}]
					},
					code: {
						rules: [{
							checkType: "required",
							errorMsg: "请输入验证码"
						}]
					}
				};
				var formData = e.detail.value;
				var checkRes = self.$zmmFormCheck.check(formData, rules);
				formData.password = self.$md5.hex_md5(formData.password)
                formData.inviteCode = self.inviteCode;
				if (checkRes) {
					if (!self.agree) {
						uni.showToast({
							title: '请先同意《隐私及服务协议》',
							icon: 'none'
						});
						return;
					}
					self.$http.request({
						url: '/auth/register',
						method: 'POST',
						data:JSON.stringify(formData),
						success: (res) => {
							if (res.data.code == 200) {
								uni.showToast({
									title:'注册成功',
									complete() {
										setTimeout(()=>{
											self.goDown();
										},1500)
									}
								})
							}
						}
					});
				} else {
					uni.showToast({
						title: self.$zmmFormCheck.error,
						icon: "none",
						position: 'bottom'
					});
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	.wx-login-title {
		text-align: center;
		padding-top: 120rpx;
		font-size: 42rpx;
		padding-bottom: 24rpx;
	}
	.xw-login-form {
		padding: 34rpx;
	}
	.xw-login-form-item {
		position: relative;
		padding: 0 12rpx;
		border-bottom: 1px #eee solid;
		display: flex;
		flex-direction: row;
		align-items: center;
		line-height: 100rpx;
		height: 100rpx;
	}
	.xw-login-form-label {
		width: 160rpx;
		min-width: 160rpx;
	}
	.xw-login-form-input {}
	.login-agree {
		margin-top: 34rpx;
		justify-content: center;
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.login-agree-text,
	.login-agree-btn {
		font-size: 24rpx;
		color: #222;
	}
	.login-agree-text {
		color: #8295a5;
	}
	.login-form-icon {
		width: 50rpx;
		height: 50rpx;
	}
	.login-form-seepass {
		position: absolute;
		right: 20rpx;
		top: 50%;
		transform: translateY(-50%);
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}
	.login-form-icon image {
		float: left;
		width: 50rpx;
		height: 50rpx;
	}
	.xw-login-form-code {
		padding: 0 12rpx;
		height: 100rpx;
		line-height: 100rpx;
		color: #8295a5;
	}
	.wx-btn {
		min-width: 200rpx;
		height: 75rpx;
		line-height: 75rpx;
		text-align: center;
		border-radius: 12rpx;
		background-color: #007AFF;
		color: #fff;
		font-size: 32rpx;
	}
	.wx-btn-info {
		background-color: #05C160;
	}
	.xw-login-form-btn {
		width: 50%;
		height: 80rpx;
		line-height: 80rpx;
		margin-top: 120rpx;
	}
	.xw-login-form-btn_h{
		width: 50%;
		height: 80rpx;
		line-height: 80rpx;
		margin-top: 30rpx;
		background: #f5f5f5;
		color: #000000;
	}
</style>